#include "delay.h"
#include "pid.h"
static volatile uint32_t TimingDelay = 0;            // 统计发生中断的次数
static volatile uint32_t Increasetick = 0;          // 每次中断就加1
// 减到0时，运行的中断服务函数,1ms运行一次这个函数
void SysTick_Handler(void)
{
   if (TimingDelay != 0)            // 如果TimingDelay不等于0，每次中断就减1
        TimingDelay = TimingDelay - 1;
   Increasetick = Increasetick + 1;
	 
	 Vertical_Contrl();
}

// 配置延时
void SysTick_Init(void)
{
    /* SystemFrequency / 1000    1ms中断一次
     * SystemFrequency / 100000  10us中断一次
     * SystemFrequency / 500000        2us中断一次
     */
    if (SysTick_Config(SystemCoreClock / 1000))
    {
        /* Capture error */
        while (1)
            ;
    }
    // 开启滴答定时器,减1减到0就发生中断
    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
}


// 延时毫秒
void  delay_ms(int n)
{
    TimingDelay = n;        // 求延时n微秒时，要发生多少次中断
        while(TimingDelay != 0);    // 等待TimingDelay等于0，当TimingDelay等于0时，延时时间到了
}

uint32_t get_tick(unsigned long *tick)
{
	if (tick != NULL)
		*tick = Increasetick;
	return Increasetick;
}
